//
//  HomeMapViewController.m
//  成都咖啡
//
//  Created by rimi on 16/4/7.
//  Copyright © 2016年 wenhao. All rights reserved.
//

#import "HomeMapViewController.h"
#import "annoatation.h"
#import <MapKit/MapKit.h>
#import <BmobSDK/Bmob.h>

@interface HomeMapViewController ()
{
    NSArray *data;
    NSMutableArray * arraydata;
    NSMutableArray * arrayLoad;
    NSDictionary * dic;
    NSDictionary * pointDic;
    BmobGeoPoint * point;
 
}
@property(nonatomic,strong)NSMutableArray * datasources;
@end

@implementation HomeMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getdatasources];
    
   

//    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(31.89, 112.78);
//    annoatation *annotation = [[annoatation alloc] init];
//    annotation.coordinate = coordinate;
//   // NSLog(@"$$%@",_arrayData);
//    annotation.title = @"成都市高新区孵化园";
//    [self.mapView addAnnotation:annotation];
//    [self.mapView setRegion:MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000)];
    }


-(void)getdatasources
{
    
    ///拿到属于这个cell分类的shop数组
    BmobQuery *query = [BmobQuery queryWithClassName:@"ShopInfo"];
    //在分类约束下的值：在ShopInfo表里，shopTag里面不同tag值的数组bquery
    
    //    [query whereKey:@"shopName" equalTo:_shopListString];
    
    //查找GameScore表的数据
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            NSLog(@"没有得到数据源");
        } else {
            data = array;
            // NSLog(@"%@",array);
            _datasources = [NSMutableArray array];
            
            for (BmobObject * obj in array) {
                //                NSLog(@"%@",obj);
                /////           NSLog(@"%@",[obj objectForKey:@"longitude_latitude"]);
                // NSLog(@"shopName = %@",[obj objectForKey:@"shopName"]);
                arraydata = [NSMutableArray array];
                NSString * stringName =[obj objectForKey:@"shopName"];
                [arraydata addObject: stringName];
                
                
                //
                [arraydata addObject:[obj objectForKey:@"longitude_latitude"]];
                
                // NSLog(@"%@",[obj objectForKey:@"longitude_latitude"]);
                point  = [obj objectForKey:@"longitude_latitude"];
                
                pointDic = @{@"long":@(point.longitude), @"lat": @(point.latitude)};
                //                CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(point.latitude, point.longitude);
                // NSLog(@"%lf_%lf",loc.latitude,loc.longitude);
                //                dic = [NSDictionary dictionary];
                //                dic = @{@"name":stringName,@"load":pointDic,@
                //                        "address":stringAddress};
                
                
                
                
                dic = [NSDictionary dictionary];
                dic = @{@"name":stringName,@"load":pointDic};
                //  NSLog(@"%@",dic);
                [_datasources addObject:dic];
                
//                NSLog(@"^^^^^^^^^^^^^^^^^^^^^^^^%@",_datasources);
                
                
                
                for (int i = 0; i<_datasources.count; i++) {
                   

                    CLLocationCoordinate2D cooor = CLLocationCoordinate2DMake([_datasources[i][@"load"][@"lat"]doubleValue], [_datasources[i][@"load"][@"long"]doubleValue]);
                     annoatation * annta = [[annoatation alloc]init];

                    annta.coordinate =cooor;
                    
                            annta.title = _datasources[i][@"name"];
                    
                            [self.mapView addAnnotation:annta];
                    
                             [self.mapView setRegion:MKCoordinateRegionMakeWithDistance(cooor, 2000, 2000)];
                    
                    
                    
                }
            }
            
           //  NSLog(@"%lu",(unsigned long)_datasources.count);
            
            
        }
        
        
        
    }];
    
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
  
    MKAnnotationView *newAnnotation=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation1"];
//    UIButton * annbutton= [UIButton buttonWithType:UIButtonTypeSystem];
//    annbutton.frame = CGRectMake(0, 0, 30, 30);
//    annbutton.backgroundColor = [UIColor redColor];
//    annbutton.imageView.image = [UIImage imageNamed:@"iconfont-xinlinghuayuan"];
//    [annbutton addTarget:self action:@selector(respondsAnnbutton:) forControlEvents:UIControlEventTouchUpInside];
//    newAnnotation.rightCalloutAccessoryView = annbutton;
    newAnnotation.image = [UIImage imageNamed:@"Location_Pin_32px_1187228_easyicon.net"];
    newAnnotation.canShowCallout=YES;
    return newAnnotation;
}

//-(void)respondsAnnbutton:(UIButton * )sender
//{
//    NSLog(@"返回了店铺信息页面");
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
